package com.yoda.youdianda.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.sun.corba.se.impl.orbutil.ObjectStreamClassUtil_1_3;
import com.yoda.youdianda.common.ErrorResult;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.common.RC;
import com.yoda.youdianda.common.ThreadContext;
import com.yoda.youdianda.domain.SysMenu;
import com.yoda.youdianda.domain.SysUser;
import com.yoda.youdianda.domain.YddUser;
import com.yoda.youdianda.dto.UserDto;
import com.yoda.youdianda.exception.BusinessException;
import com.yoda.youdianda.exception.RegException;
import com.yoda.youdianda.mapper.YddUserMapper;
import com.yoda.youdianda.service.SystemService;
import com.yoda.youdianda.service.YddArticleService;
import com.yoda.youdianda.util.JwtUtils;
import com.yoda.youdianda.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.lang.invoke.LambdaConversionException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author shawn
 * @date 2022年 11月 12日 20:18
 */
@Slf4j
@RestController
@RequestMapping("/index")
public class SystemController {

    @Autowired
    private SystemService systemService;
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddUserMapper yddUserMapper;


    /**
     * 获取配置
     *
     * @return {@link R}<{@link SysConfigVo}>
     */
    @GetMapping("/getConfig")
    public R<SysConfigVo> getConfig() {
        return systemService.getConfig();
    }


    /**
     * 获取后台基本信息
     *
     * @return {@link R}
     */
    @GetMapping("/baseInfo")
    public R getBaseInfo() {
        BaseInfoVo vo = systemService.getBaseInfo();
        return R.success(vo);
    }


    /**
     * 登录
     *
     * @param userDto 用户dto
     * @return {@link R}
     */
    @PostMapping("login")
    public R login(@RequestBody UserDto userDto) {
        log.info("接收到用户登录信息:{}", userDto.toString());
        //获取该用户对象
        if (Objects.isNull(userDto.getLogin_name()) || Objects.isNull(userDto.getPassword())) {
            throw new BusinessException(ErrorResult.nullError());
        }
        SysUser sysUser = systemService.selectUserByLoginName(userDto.getLogin_name());
        //密码md5加密鉴权
        String md5 = SecureUtil.md5(userDto.getPassword());
        if (Objects.isNull(sysUser) || !md5.equals(sysUser.getPassword())) {
            //暂无该用户/密码错误,登录失败!
            throw new BusinessException(ErrorResult.loginError());
        }
        //修改当前用户登录时间
        sysUser.setLoginDate(new Date());
        systemService.updateUser(sysUser);
        //登录成功,封装为json对象
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", sysUser.getId());
        claims.put("loginName", sysUser.getLoginName());
        String token = JwtUtils.getToken(claims);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        return R.success(map);
    }

    /**
     * 获取用户资料
     *
     * @return {@link R}
     */
    @PostMapping("/profile")
    public R getProfile() {
        //获取用户id
        String userId = ThreadContext.getId(RC.SYS_ID);
        //根据用户id装配vo对象
        ProfileVo vo = systemService.getProfile(userId);
        return R.success(vo);
    }


    /**
     * 前台:推荐文章
     *
     * @return
     */
    @GetMapping("/recommend")
    public R recommend() {
        RArticlessVo recommend = yddArticleService.recommend();
        return R.success(recommend, 0);
    }

    /**
     * 前台:首页和全局
     *
     * @return
     */
    @GetMapping("/index")
    public R index() {
        HomePageVo index = systemService.getIndex();
        return R.success(index, 0);
    }

    /**
     * 前台热门置顶文章数据
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/new")
    public R news(String page, String limit) {
        HotVo getnew = systemService.getnew(page, limit);
        return R.success(getnew, 0);
    }

    /**
     * 前台文章列表数据
     *
     * @param page
     * @param limit
     * @param cateid
     * @return
     */
    @GetMapping("/list")
    public R list(String page, String limit, String cateid) {
        ArticleListDataVo getlist = systemService.getlist(page, limit, cateid);
        return R.success(getlist, 0);
    }

    /**
     * 前台最新文章
     *
     * @return
     */
    @GetMapping("/hot")
    public R hot() {
        RArticlessVo gethot = systemService.gethot();
        return R.success(gethot, 0);
    }

    /**
     * 文章详情数据
     */
    @GetMapping("/show")
    public R show(Integer id) {
        ArticleDetailsDataVo getshow = systemService.getshow(id);
        return R.success(getshow, 0);
    }

    /**
     * 前台注册
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/reg")
    public R<String> reg(@RequestBody YddUser yddUser) {
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getMobile, yddUser.getMobile());
        YddUser yddUser1 = yddUserMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(yddUser1)) {
            throw new RegException(ErrorResult.regError());
        }
        yddUser.setUsername(yddUser.getMobile());
        yddUser.setSex(1);
        yddUserMapper.insert(yddUser);
        return R.success("注册成功", 0);
    }
}
